﻿'use strict';
app.controller('DirectsponsorController', ['$rootScope', '$scope', '$state', '$localStorage', '$http', '$modal', '$timeout', 'TreeFactory', 'commonFactory', function ($rootScope, $scope, $state, $localStorage, $http, $modal, $timeout)
{
        $scope.messeger = "",
        $scope.title = "",
        $scope.popup = function (windowClass, StyleModal, Controller, size) {
            $modal.open({
                windowClass: windowClass,
                templateUrl: StyleModal,
                controller: Controller,
                size: size,
                resolve: {
                    messeger: function () {
                        return $scope.messeger;
                    },
                    title: function () {
                        return $scope.title;
                    }
                }
            })
        },
        $scope.nottree = false;
        $scope.UserNew = {
            Sponsor: '', Sponsor2: '', UserName: '',HoTen: '',SoCMND:'', Email: '', Password: '', Password2: '', SDTNHARIENG: '',
            SponsorID : 0 , Sponsor2ID : 0,isLeft : false,BankId :0 , SecondPassword: '',ConfirmSecondPassword:''
        };
        $scope.downline = {
            UserName: $localStorage.user.UserName,
            HoTen: $localStorage.user.HoTen,
        },
        $scope.bank = {},
        $scope.BankList = [],
        $scope.selected = [],
        $scope.Info =
        {
            Id : 0,
            Downline: "0",
            TotalF1: "0",
            Invest: "0",
            SponsorId: 0,
            SponsorName: '',
            Sponsor2Id: 0,
            Sponsor2Name: '',
            Level: "0",
            UserName: '',
            FullName :''
        },
        $scope.bank.selected = { Name: 'VietComBank',Id: 1 },
        getInforByUserLogin($localStorage.user.UserId),
        $scope.doLoadTree = function () {
            getTree();          
        },
        $scope.ToAddUser = function (downline) {
            $scope.UserNew.Sponsor = $localStorage.user.UserName;       
            doCheckPin($localStorage.user.UserId);
        },
        $scope.SaveUser = function (UserNew) {
            $scope.progress = true;
            doAddUser(UserNew)
        },
        getTree();
        GetBankList();
        function getTree() {
            $('#tree')
                .on('changed.jstree', function (e, data) {                                        
                    var i, j, r = [];
                    for (i = 0, j = data.selected.length; i < j; i++)
                    {
                        $scope.UserNew.Sponsor2 = data.instance.get_node(data.selected[i]).UserName;                      
                        GetDocument(data.instance.get_node(data.selected[i]).id);
                        //$scope.downline = data.instance.get_node(data.selected[i]).data;
                    } 
                })
                .jstree({
                'core': {
                    'data': {
                        'url': function (node) {
                            return node.id === '#' ?
                              $rootScope.API + 'JsonAccount/getRootTree' :
                              $rootScope.API + 'JsonAccount/getTreeChildren';
                        },
                        'data': function (node) {
                            if (node.id === "#") {                                
                                return { 'id': $localStorage.user.UserName };
                            }
                            return { 'id': node.id };
                        },
                    },                    
                    'strings' : {
		 				'Loading ...' : 'Please wait loading node data...'
                    },                   
                  }
                });
        };
        function GetBankList() {
            commonFactory.getBankList().then(function (rsp) {
                if (rsp.data.Status == true)
                {
                    $scope.BankList = rsp.data.Result;
                }
            });
        }
        function GetDocument(id) {
            commonFactory.getDocument(id).then(function (rsp) {
                $scope.Info = rsp.data.doc;
            });
        };
        function doAddUser(UserNew)
        {
            commonFactory.Register(UserNew).then(function (response) {
                $scope.progress = false;
                if (response.data.Status) {
                    $scope.AlertType = "success";
                    $scope.messeger = response.data.Messeger;
                    $timeout(function () {
                        $state.reload();
                    }, 3600);
                } else {
                    $scope.messeger = response.data.Messeger;
                }
            }, function (error) {
                $scope.progress = false;
                $scope.messeger = 'Unable to load profile data: ' + error.message;
            });
        };
        function doCheckPin(userId)
        {
            commonFactory.checkCountPin(userId).then(function (rsp) {
                if (rsp.data.Status == false)
                {
                    $scope.messeger = "You must be greater than 4 pin";
                    $scope.title = 'Warning';
                    $modal.open({
                        windowClass: "modal-message modal-warning",
                        templateUrl: "WarningModal",
                        controller: "ModalInstanceCtrl",
                        size: "sm",
                        resolve: {
                            messeger: function () {
                                return $scope.messeger;
                            },
                            title: function () {
                                return $scope.title;
                            },
                        }
                    })
                }else
                {
                    $scope.ShowAddForm = true;
                }
            });
        };
        function getInforByUserLogin(id) {
            commonFactory.getInforByUserLogin(id).then(function successCallback(resp) {
                $scope.Info.TotalF1 = resp.data.F1;
                $scope.Info.Invest = resp.data.Invest;
                $scope.Info.Downline = resp.data.Total;
                $scope.Info.TotalPin = resp.data.TotalPin;
                $scope.Info.Level = resp.data.Level;
                $scope.Info.UserName = resp.data.UserName;
                $scope.Info.FullName = resp.data.FullName;
            }, function (error) {
            });
        }
    }
]);
app.controller('ModalInstanceCtrl', function ($scope, $modalInstance, $state, messeger, title) {
    $scope.messeger = messeger,
    $scope.title = title;
    $scope.ok = function () {
        $modalInstance.close();
    };
    $scope.cancel = function () {
        $modalInstance.dismiss('cancel');
    };
});
